package com.qwiki.xml;

import java.io.PrintStream;
import java.util.Arrays;

public class WikiPageAsyncStreamHandler extends WikiPageAbstractAsyncHandler {
  private WikiXMLPageFilter filter;
  private final PrintStream os;

  /**
   * @param name
   * @param id
   */
  public WikiPageAsyncStreamHandler(String name, int id, PrintStream os) {
    super(name, id);
    this.os = os;
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * com.qwiki.xml.WikiPageAbstractAsyncHandler#doProcess(com.qwiki.xml.WikiXMLPage
   * )
   */
  @Override
  public void doProcess(WikiXMLPage xmlpage) {
    if (filter == null || filter.isGood(xmlpage)) {
      try {
        WikiPage page = xmlpage.parse();
//        os.format("%1$s\n%2$s\n%3$s\n", page.getTitle(), "",
//            Arrays.toString(page.getCategories()));
        for(String cat: page.getCategories()){
          os.print(cat.toLowerCase());
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see com.qwiki.xml.WikiPageHandler#getFilter()
   */
  @Override
  public synchronized WikiXMLPageFilter getFilter() {
    // TODO Auto-generated method stub
    return filter;
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * com.qwiki.xml.WikiPageHandler#setFilter(com.qwiki.xml.WikiXMLPageFilter)
   */
  @Override
  public synchronized void setFilter(WikiXMLPageFilter filter) {
    this.filter = filter;
  }

}
